package com.desksoft.ecommerce.vo.mall;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


public class BuyCartVO extends BuyItemVO {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5252137243619981384L;
	
	/**
	 * 原价总金额
	 */
	private float stotalPrice;


	/**
	 *  添加购物项
	 * @param item
	 */
	public void addItem(BuyItemVO item){
		if(!items.contains(item)){
			items.add(item);
			item.setSubtotal(getSubtotalPrice(item));
		}else{//如果已经存在该购物项,就累加其购买数量
			for(BuyItemVO bi : items){
				if(bi.equals(item)){
					bi.setCamount(bi.getCamount()+item.getCamount());
					bi.setWholeqty(bi.getWholeqty()+item.getWholeqty());
					bi.setSubqty(bi.getSubqty()+item.getSubqty());
					bi.setSubtotal(getSubtotalPrice(bi));
					break;
				}
			}
		}
	}
	
	/**
	 *  2C添加购物项
	 * @param item
	 */
	public void add2cItem(BuyItemVO item){
		if(!items.contains(item)){
			items.add(item);
			item.setSubtotal(getSubtotalPrice(item)); 
		}else{//如果已经存在该购物项,就累加其购买数量
			for(BuyItemVO bi : items){
				if(bi.equals(item)){
					bi.setCamount(bi.getCamount()+item.getCamount());
					bi.setSubtotal(getSubtotalPrice(bi));
					break;
				}
			}
		}
	}
	
	/**
	 * 修改商品数量
	 * @param itemcode
	 * @param qty
	 */
	public void updateItem(String itemcode,int qty,int wholeqty,int subqty){
		for(BuyItemVO bi : items){
			if(bi.getProductsinfo().getItemCode().equals(itemcode)){
				bi.setCamount(qty);
				bi.setWholeqty(wholeqty);
				bi.setSubqty(subqty);
				bi.setSubtotal(getSubtotalPrice(bi));
				break;
			}
		}	
	}
	
	/**
	 * 2c修改商品数量
	 * @param itemcode
	 * @param qty
	 */
	public void updateItem(String itemcode,int qty){
		for(BuyItemVO bi : items){
			if(bi.getProductsinfo().getItemCode().equals(itemcode)){
				bi.setCamount(qty);
				bi.setSubtotal(getSubtotalPrice(bi));
				break;
			}
		}	
	}
	
	/**
	 * 增减购买数量 
	 * @param buyCartVO
	 */
	public void plusorminusItem(String itemcode , int qty,int wholeqty,int subqty){
		for(BuyItemVO bi : items){
			if(bi.getProductsinfo().getItemCode().equals(itemcode)){
				if(qty < 0 && bi.getCamount() == 1){
					break;
				}
				bi.setCamount(bi.getCamount()+qty);
				bi.setWholeqty(bi.getWholeqty()+wholeqty);
				bi.setSubqty(bi.getSubqty()+subqty);
				bi.setSubtotal(getSubtotalPrice(bi));
			}
		}
	}
	
	/**
	 * 2c增减购买数量 
	 * @param buyCartVO
	 */
	public void plusorminusItem(String itemcode , int qty){
		for(BuyItemVO bi : items){
			if(bi.getProductsinfo().getItemCode().equals(itemcode)){
				if(qty < 0 && bi.getCamount() == 1){
					break;
				}
				bi.setCamount(bi.getCamount()+qty);
				bi.setSubtotal(getSubtotalPrice(bi));
			}
		}
	}
	
	/**
	 * 清除所有项
	 */
	public void removeAll(){
		items.clear();
	}
	
	/**
	 * 删除指定项
	 * @param buyCartVO
	 */
	public void removeBuyItem(BuyItemVO buyItemVO){
		if(items.contains(buyItemVO)){
			items.remove(buyItemVO);
		}
	}
	
	/**
	 * 删除多个指定项
	 * @param buyCartVO
	 */
	public void removeBuyItems(String[] itemcodes){
		List<String> itemList = new ArrayList<String>();
		itemList = java.util.Arrays.asList(itemcodes);
		for(String itemcode : itemList){
			BuyItemVO buyitem = new BuyItemVO(new ProductsInfo(itemcode));
			if(items.contains(buyitem)){
				items.remove(buyitem);
			}
		}
	}
	
	/**
	 * 获取单项商品的小计金额
	 * @param buyItemVO
	 * @return
	 */
	public float getSubtotalPrice(BuyItemVO buyItemVO){
		float subtotal = 0f;
		for(BuyItemVO bi : items){
			if(bi.equals(buyItemVO)){
					if(bi.getProductsinfo().getDayprice() != null){
						subtotal =((BigDecimal)bi.getProductsinfo().getDayprice()).floatValue()*bi.getCamount();
					}else{
						subtotal =((BigDecimal)bi.getProductsinfo().getPrice()).floatValue()*bi.getCamount();
					}
				}
			}
		return subtotal;
	} 
	
	/**
	 * 购物车商品总数（不包括未选中商品）
	 * @return
	 */
	public int getTotalAmount(){
		int totalAmount = 0;
		for(BuyItemVO bi : items){
			if(bi.isCheckitem()){
				totalAmount+=bi.getCamount();
			}
		}
		return totalAmount;
	}
	
	/**
	 * 购物车商品总数(包括末选中商品)
	 * @return
	 */
	public int getTotalAmountNoCountClick(){
		int totalAmount = 0;
		for(BuyItemVO bi : items){
				totalAmount+=bi.getCamount();
		}
		return totalAmount;
	}
	/**
	 * 获取商品总金额
	 * @return
	 */
	public float getTotalPrice(){
		
		float totalPrice = 0f;
		for(BuyItemVO bi : items){
			if(bi.isCheckitem()){
			if(bi.getProductsinfo().getDayprice() != null){
				totalPrice += ((BigDecimal)bi.getProductsinfo().getDayprice()).floatValue() * bi.getCamount();
			}else{
				totalPrice += ((BigDecimal)bi.getProductsinfo().getPrice()).floatValue() * bi.getCamount();
			}
			}
		}
		return totalPrice;
	}

	public void setStotalPrice(float stotalPrice) {
		this.stotalPrice = stotalPrice;
	}

	public float getStotalPrice() {
		this.stotalPrice = 0f;
		for(BuyItemVO bi : items){
			if(bi.isCheckitem()){
				stotalPrice += ((BigDecimal)bi.getProductsinfo().getPrice()).floatValue() * bi.getCamount();
			}
		}
		return stotalPrice;
	}
	
}
